/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.curso.dao.serializables;

import java.io.*;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Andres
 */
public abstract class GenericDaoSer <T, PK extends Serializable>{
    public abstract T save(T t);
    public abstract List<T> getAll();



    public T persist(T bo) {

        List<T> allObjects = null;

        //archivo existe?
        File file = new File(getFileName(bo.getClass()));
        if(file.exists()){
            //Obtenemos la colleccion anteriormente guardada
            allObjects = getAll(bo.getClass());
        }else{
            allObjects = new ArrayList<T>();
        }

        //Agregamos el objeto a serializar
        allObjects.add(bo);

        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        try {
            fos = new FileOutputStream(file);
            out = new ObjectOutputStream(fos);
            out.writeObject(allObjects);
            out.close();
        } catch(IOException ex) {
            ex.printStackTrace();
        }
        return bo;
    }

    public  List<T> getByClassName(Class clazz ){
         
        FileInputStream fis = null;
        ObjectInputStream in = null;
        List<T> list = null ;
        try {
            fis = new FileInputStream(getFileName(clazz));
            in = new ObjectInputStream(fis);
            list = (ArrayList) in.readObject();
            in.close();

        } catch(IOException ex) {
            ex.printStackTrace();
        } catch(ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return list;
        
    }

    private Properties getConfiguration() {
        Properties p = new Properties();
        try {
            p.load(getClass().getClassLoader().getResourceAsStream("configuration.properties"));
        } catch (IOException ex) {
            Logger.getLogger(GenericDaoSer.class.getName()).log(Level.SEVERE, null, ex);
        }
        return p;
    }

    public List<T> getAll(Class clazz){

        FileInputStream fis = null;
        ObjectInputStream in = null;
        List listResult = new ArrayList();
        try {
            fis = new FileInputStream(getFileName(clazz));
            in = new ObjectInputStream(fis);
            listResult = (ArrayList)in.readObject();
            in.close();

        } catch(FileNotFoundException ex){
            ex.printStackTrace();
        }catch(IOException ex) {
            ex.printStackTrace();
        } catch(ClassNotFoundException ex) {
            ex.printStackTrace();
        }

        return listResult;
    }

    private String getFileName(Class clazz){

        StringBuilder filename =new StringBuilder()
                .append(getConfiguration().getProperty("dao.savefiles"))
                .append(clazz.getSimpleName())
                .append("s.ser");
        return filename.toString();
    }


        public Boolean remove(T t){

            List<T> list =  getAll(t.getClass());
            Boolean res =false;


            Iterator it = list.iterator();

            while(it.hasNext()){
            Object o  =  it.next();
            if(t.equals(o))
                it.remove();
            }
            /*
            for(T o : list){
                if(t.equals(o)){
 
                        list.remove(o);

                    res=true;
                }
            }*/
         

            File file=new File(getFileName(t.getClass()));
            if(!file.delete()){
                System.out.println("No se ha eliminado el archivo  " + file.getName());
            }

            FileOutputStream fos = null;
            ObjectOutputStream out = null;
            try {
                fos = new FileOutputStream(file);
                out = new ObjectOutputStream(fos);
                out.writeObject(list);
                out.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            return res;

        }
}
